<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Think\Exception;
use Think\Model;

class BusinessactivityController extends AdminbaseController{
    
	protected $businessActivityModel;
	
	public function _initialize() {
		parent::_initialize();
		$this->businessActivityModel = M("BusinessActivity");
	}

    //商户活动列表
	public function index(){
        $where=['ba.is_deleted'=>0,'end_time'=>['EGT',time()],'b.status'=>1,'b.is_checked'=>1];
        $name=I('name');
        $business_name=I('business_name');
        if($name){
            $where['ba.name']=$name;
        }
        if($business_name){
            $where['b.name']=$business_name;
        }
        $sql=$this->businessActivityModel
            ->alias('ba')
            ->join('LEFT JOIN __BUSINESS__ as b ON ba.business_id=b.id')
            ->where($where)
            ->field('ba.id,start_time,end_time,ba.name,b.name as business_name,ba.business_id')
            ->order('end_time')
            ->fetchSql()
            ->select();
        $model = new Model();
        $count=$model->query('SELECT `id`  FROM ('.$sql.') AS a group by `business_id` ');
        $count=count($count);
        $page = $this->page($count, 20);
        $model = new Model();
        $businessActivities=$model->query('SELECT * FROM ('.$sql.') AS a group by `business_id` limit '.$page->firstRow.','.$page->listRows );
        $this->assign("businessActivities",$businessActivities);
        $this->assign("page", $page->show('Admin'));
        $this->display();
	}

    //活动详情
    public function getDetails(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('缺少参数');
        }
        $businessActivities=$this->businessActivityModel
            ->alias('ba')
            ->join('LEFT JOIN __BUSINESS__ as b ON ba.business_id=b.id')
            ->join('LEFT JOIN __BUSINESS_ACTIVITY_CONTENT__ as bac ON ba.business_id=bac.business_id')
            ->where(['ba.is_deleted'=>0,'end_time'=>['EGT',time()],'ba.id'=>$id,'b.status'=>1,'b.is_checked'=>1])
            ->field('ba.id,bac.logo,bac.image,bac.content,start_time,end_time,ba.name,inventory,ba.price,ba.order_number,b.name as business_name')
            ->find();
        $this->assign("businessActivities",$businessActivities);
        $this->display('getDetails');
    }

    //添加活动轮播图
    public function addActivityRecommend(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('缺少参数');
        }
        $businessActivities=$this->businessActivityModel
            ->alias('ba')
            ->join('LEFT JOIN __BUSINESS__ as b ON ba.business_id=b.id')
            ->where(['ba.is_deleted'=>0,'end_time'=>['EGT',time()],'ba.id'=>$id,'b.status'=>1,'b.is_checked'=>1])
            ->field('ba.id')
            ->find();
        if($businessActivities){
            if(IS_POST){
                $count=M('BusinessActivityRecommend')->count('id');
                if(intval($count)>=5){
                    $this->error('最多只能添加5个轮播推荐');
                }
                $image=I('image');
                if(!$image){
                    $this->error('图片必须');
                }
                $data['image']=$image;
                $data['business_activity_id']=$id;
                $data['create_time']=time();
                $result=M('BusinessActivityRecommend')->add($data);
                if($result){
                    $this->success('添加成功',U('Businessactivity/index'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->assign('id',$id);
                $this->display('addActivityRecommend');
            }
        }else{
            $this->error('活动已结束或商户被禁用');
        }
    }

    //活动轮播图列表
    public function recommend(){
        $recommends=M('BusinessActivityRecommend')
            ->alias('bar')
            ->join('LEFT JOIN __BUSINESS_ACTIVITY__ as ba ON ba.id=bar.business_activity_id')
            ->join('LEFT JOIN __BUSINESS__ as b ON b.id=ba.business_id')
            ->field('bar.image,b.name as business_name,ba.name,ba.start_time,ba.end_time,bar.id')
            ->select();
        $this->assign('recommends',$recommends);
        $this->display();
    }

    //活动轮播图编辑
    public function recommendEdit(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('缺少参数');
        }
        $recommend=M('BusinessActivityRecommend')->where(['id'=>$id])->find();
        if($recommend){
            if(IS_POST){
                $image=I('image');
                if(!$image){
                    $this->error('图片必须');
                }
                $result=M('BusinessActivityRecommend')->where(['id'=>$id])->save(['image'=>$image]);
                if($result!==false){
                    $this->success('更新成功',U('Businessactivity/recommend'));
                }else{
                    $this->error('更新失败');
                }
            }else{
                $this->assign('recommend',$recommend);
                $this->display('recommendEdit');
            }
        }else{
            $this->error('推荐轮播不存在');
        }
    }

    //活动轮播删除
    public function recommendDelete(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('缺少参数');
        }
        $recommend=M('BusinessActivityRecommend')->where(['id'=>$id])->find();
        if($recommend){
            $result=M('BusinessActivityRecommend')->where(['id'=>$id])->delete();
            if($result!==false){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }else{
            $this->error('推荐轮播不存在');
        }
    }
}